package com.jt.service;

import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private ItemCatMapper itemCatMapper;

    //    @Override
//    public List<ItemCat> findItemCatList(Integer level) {
//        //1.查询一级商品分类信息
//        long startTime = System.currentTimeMillis();
//        int parentId = 0;
//        List<ItemCat>oneList = itemCatMapper.findItemCatByParentId(parentId);
//        //2.查询二级商品分类信息
//        for(ItemCat oneItemCat : oneList){
//            //2.1 根据一级的id 查询二级的数据
//           int oneId = oneItemCat.getId();
//           List<ItemCat> twoList = itemCatMapper.findItemCatByParentId(oneId);
//            for(ItemCat twoItemCat :twoList) {
//                int twoId = twoItemCat.getId();
//                List<ItemCat> threeList = itemCatMapper.findItemCatByParentId(twoId);
//                twoItemCat.setChildren(threeList);
//            }
//           oneItemCat.setChildren(twoList);
//        }
//        long endTime = System.currentTimeMillis();
//        System.out.println("耗时"+(endTime - startTime)+"毫秒");
//        return oneList;
//}
    public Map<Integer, List<ItemCat>> getMap() {
        Map<Integer, List<ItemCat>> map = new HashMap<>();
        //1.查询所有的数据库信息
        List<ItemCat> list = itemCatMapper.findItemCatList();
        //2.遍历数据封装Map
        for (ItemCat itemCat : list) {
            int key = itemCat.getParentId();
            if (map.containsKey(key)) {
                map.get(key).add(itemCat);
            } else {//key不存在，第一个数据
                List<ItemCat> temList = new ArrayList<>();
                temList.add(itemCat);
                map.put(key, temList);
            }
        }
        return map;
    }

    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        long startTime = System.currentTimeMillis();
        Map<Integer,List<ItemCat>> map = getMap();

        //level=1 只查询一级数据
        if(level == 1){
            return map.get(0);
        }
        if(level == 1)
            return map.get(0);

        //level=2 查询一级和二级数据
        if(level == 2)
            return getTwoList(map);

        //return null;
        //level=3 查询一级和二级和三级数据
        List<ItemCat> threeList = getThreeList(map);
        long endTime = System.currentTimeMillis();
        System.out.println((endTime - startTime)+"毫秒");
        return threeList;
    }
    /**
     * 思路:
     *     1.获取三级商品分类信息
     *     2.先应该获取2级商品分类信息
     * @param map
     * @return
     */
    private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map) {
        //1.先查询一级和二级的数据
        List<ItemCat> oneList = getTwoList(map);
        //2.遍历一级集合,获取二级数据
        for (ItemCat oneItemCat : oneList){
            //2.1 获取二级数据信息, 二级数据可能为null
            List<ItemCat> twoList = oneItemCat.getChildren();
            if(twoList == null || twoList.size()==0)
                continue;

            for (ItemCat twoItemCat : twoList){
                int twoId = twoItemCat.getId();
                //2.2获取三级集合信息
                List<ItemCat> threeList = map.get(twoId);
                //2.3将三级数据封装到二级对象中
                twoItemCat.setChildren(threeList);
            }
        }
        return oneList;
    }

    private List<ItemCat> getTwoList(Map<Integer, List<ItemCat>> map) {
        //1.查询一级数据
        List<ItemCat> oneList = map.get(0);
        for (ItemCat oneItemCat : oneList) {
            //获取一级id
            int id = oneItemCat.getId();
            //根据一级id查询二级id
            List<ItemCat> twoList = map.get(id);
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }

    @Override
    @Transactional //事务控制
    public void saveItemCat(ItemCat itemCat) {
        Date date = new Date();
        itemCat.setStatus(true)
                .setCreated(date)
                .setUpdated(date);
        itemCatMapper.saveItemCat(itemCat);
    }

    @Override
    @Transactional
    public void updateStatus(ItemCat itemCat) {
        itemCat.setUpdated(new Date());
        itemCatMapper.updateStatus(itemCat);
    }

    @Override
    public void updateItemCat(ItemCat itemCat) {
        itemCat.setUpdated(new Date());
        itemCatMapper.updateItemCat(itemCat);
    }

    @Override
    @Transactional
    public void deleteItemCat(ItemCat itemCat) {
        //判断level到底是几级菜单
        if(itemCat.getLevel() == 3){
            itemCatMapper.deleteItemCatById(itemCat.getId());
        }
        if(itemCat.getLevel() == 2){
            itemCatMapper.deleteItemCat2(itemCat.getId());
        }
        if (itemCat.getLevel() == 1){
            itemCatMapper.deleteItemCat1(itemCat.getId());
        }

    }

}